package commons.params.param_component;

import commons.params.A_ParamsManager;
import commons.utils.Utils;
import java.util.ArrayList;
import java.util.Properties;
import javax.swing.JComponent;


public abstract class AbstractParamComponent implements IParamComponent {

	protected String name;
	protected A_ParamsManager paramsManager;
	protected JComponent component;

	protected boolean bCanBeNull;
	protected boolean bIsNull;
	protected I_Activator activator;
	protected boolean bCaseSensitive = true; //utilisé surtout par les Component_simple type "nom de fichier"

	protected boolean isValid=false;
	public boolean isValide() {return isValid;} // renvoie le resultat du test, qui doit avoir ete fait avant ! (makeStandardControl)

	public AbstractParamComponent (String name, A_ParamsManager paramsManager, JComponent component, boolean bCanBeNull, I_Activator activator, boolean bCaseSensitive) {
		this.name = name;
		this.paramsManager = paramsManager;
		this.component = component;
		this.bCanBeNull = bCanBeNull;
		this.activator = activator;
		this.bCaseSensitive = bCaseSensitive;
		reinit();
	}

	public AbstractParamComponent (String name, A_ParamsManager paramsManager, JComponent component, boolean bCanBeNull) {
		this(name, paramsManager, component, bCanBeNull, new I_Activator() {
				@Override
				public boolean isActivate() {return true;}
			}, true);
	}


	// a appeler par les dérivées en début de controle
	public boolean needControl() {
		if (activator == null) {
			component.setEnabled(true);
			return true;
		}

		component.setEnabled(activator.isActivate());
		return activator.isActivate();
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public JComponent getComponent() {
		return component;
	}

	@Override
	public void invalidateComponent(String errMsg, boolean bDrawInvalidBorder) {
		paramsManager.dlg.validateOrInvalidateComponent(component, errMsg, bDrawInvalidBorder);
		isValid = false;
	}
	@Override
	public void validateComponent() {
		paramsManager.dlg.validateOrInvalidateComponent(component, "", true);
		isValid = true;
	}


	@Override
	public boolean isNull() {
		return bIsNull;
	}

	@Override
	public void reinit() {
		setValue(null);
	}

	@Override
	public void setPossibleValues(ArrayList<String> possibleValues) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void setPossibleValues(ArrayList<String>[] possibleValues) {
		throw new UnsupportedOperationException("Not supported yet.");
	}



	protected String getErrorMsg_ParamNotFound(String propertieName) {
		return "Le paramètre [" + propertieName + "] n'a pas été trouvé dans le fichier de paramétrages.";
	}
	protected String getErrorMsg_ParamNull(String propertieName) {
		return "Le paramètre [" + propertieName + "] est obligatoire et n'a aucune valeur dans le fichier de paramétrage.";
	}
	protected String getErrorMsg_ParamMustBePositiveInt(String propertieName, String badVal) {
		return "Le paramètre [" + propertieName + "] doit être un entier positif mais sa valeur est [" + badVal + "].";
	}


	protected boolean isThereProperty(Properties props, String propertyName) {
		return props.containsKey(propertyName);
	}

	// Lit et renvoie une propriété (null si introuvable)
	protected String readProperty(Properties props, String propertyName) {
		if (!props.containsKey(propertyName))
			return null;

		return props.getProperty(propertyName);
	}

	protected boolean isThereProperty_int_pos(Properties props, String propertyName) {
		if (!props.containsKey(propertyName))
			return false;
		String strVal = readProperty (props, propertyName);
		return Utils.isInteger_Pos(strVal);
	}

	protected int readProperty_int(Properties props, String propertyName) {
		String strVal = readProperty (props, propertyName);
		return Integer.parseInt(strVal);
	}
}
